using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Hanoi.Ui
{
    /// <summary>
    /// Summary description for ColumnsConfigUi.
    /// </summary>
    public class ColumnsConfigUi
    {
        private Pen p = new Pen(Color.Black);

        private Graphics g;
        private int _width;
        private int _height;
        private Bitmap _bitmap;
        private int _towerWidth;
        private int _margin = 1;
        private int _paddingHorizontal = 1;
        protected int _selected = 1;

        public int Selected
        {
            get { return _selected; }
            set { _selected = value - 1; Refresh(); }
        }

        public ColumnsConfigUi(int width, int height)
        {
            _width = width;
            _height = height;
            _towerWidth = _width / 3;

            _bitmap = new Bitmap(width, height);
            g = Graphics.FromImage(_bitmap);
        }

        public Image GetImage()
        {
            return _bitmap;
        }

        public void Refresh()
        {
            g.Clear(SystemColors.Window);

            /// Line.       _____
            PaintBottomLine();
            /// Lines.      | | |
            for (int i = 0; i < 3; i++)
                PaintStackLine(_towerWidth * (2 * i + 1) / 2, i);

        }

        private void PaintStackLine(int left, int columnIndex)
        {
            g.FillPolygon(new SolidBrush(GetColor(columnIndex)),
                new Point[] {
                    new Point(left, _margin),
                    new Point(left - 4, _height - _margin * 2),
                    new Point(left + 4, _height - _margin * 2)
                });
            g.FillRectangle(new SolidBrush(GetColor(columnIndex)),
                new Rectangle(left - 4, _height - _margin * 2, 8, 8));
        }

        private void PaintBottomLine()
        {
            g.DrawLine(p, _paddingHorizontal, _height - _margin * 2,
                _width - _paddingHorizontal * 2, _height - _margin * 2);
        }

        protected virtual Color GetColor(int columnIndex)
        {
            if (_selected == columnIndex)
                return Color.FromArgb(255, 127, 127);
            else
                return Color.FromArgb(127, 255, 127);
        }

    }
}
